/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package common;

import enums.CustomerEnum;
import enums.CarEnum;
import Swing.CarScreen;
import Swing.CarsDialog;
import dao.CustomerDao;
import dao.CarDao;
import entities.Car;
import entities.Customer;
import java.util.ArrayList;
import java.util.HashMap;

public class CarUtils implements MessagesForUser{
    CustomerDao customerDao = new CustomerDao();
    CarDao carDao = new CarDao();
    private final String SUCCESS_SAVE = "Dane zostały zapisane pomyślnie";
    private final String SUCCESS_DELETE = "Dane zostały usunięte";
    
    public void saveNewCar(HashMap<String, String> map){
        Car car = new Car();
        Customer customer = customerDao.findById(Integer.parseInt(map.get(CustomerEnum.idCustomer.name())));
        car.setCustomer(customer);
        car.setMark(map.get(CarEnum.mark.name()));
        car.setType(map.get(CarEnum.type.name()));
        car.setYearOfProduction(map.get(CarEnum.yearOfProduction.name()));
        carDao.save(car);
        sendTextForUser(SUCCESS_SAVE);
    }
    
    public ArrayList<Car> getCarsData(){
         return carDao.getAllCars();
    }

    @Override
    public void sendTextForUser(String message) {
        CarScreen carScreen = new CarScreen();
        carScreen.showDialogForUser(message);
    }
    
    public HashMap<String, String> getCarByIdAsHashMap(int id){
        HashMap<String, String> map = new HashMap<>();
       try{ 
        Car car = carDao.findById(id);
        map.put(CarEnum.mark.name(), car.getMark());
        map.put(CarEnum.type.name(), car.getType());
        map.put(CarEnum.yearOfProduction.name(), car.getYearOfProduction());
        map.put(CustomerEnum.firstName.name(), car.getCustomer().getFirstName());
        map.put(CustomerEnum.lastName.name(), car.getCustomer().getLastName());
        map.put(CustomerEnum.street.name(), car.getCustomer().getStreet());
        map.put(CustomerEnum.city.name(), car.getCustomer().getCity());
       }
       catch(Exception e){
           System.out.println(e);
       }
       return map;
    }    
    
    public void saveModifiedCar(HashMap<String, String> map){
        Car car = carDao.findById(Integer.parseInt(map.get(CarsDialog.CAR_ID)));
        car.setMark(map.get(CarEnum.mark.name()));
        car.setType(map.get(CarEnum.type.name()));
        car.setYearOfProduction(map.get(CarEnum.yearOfProduction.name()));
        carDao.update(car);
        sendTextForUser(SUCCESS_SAVE);
    }
    
    public void deleteCarFromDatabase(int carId){
        carDao.delete( carDao.findById(carId) );
        sendTextForUser(SUCCESS_DELETE);
    }
}
